<%@page import="system.Tempe"%>
<%@page import="models.GantiPassword"%>
<%@page import="models.Role"%>
<%@page import="system.Html"%>
<%@page import="models.User"%>
<h1>Update User</h1>

<%
User user = (User) request.getAttribute("user");
GantiPassword gantiPassword = (GantiPassword) request.getAttribute("gantiPassword");
%>
<% if(identity.isAdmin()) { %>
<form action="<%=request.getContextPath()%>/user/update?id=<%=user.id%>" method="POST">
    <input type="hidden" name="update_type" value="role"/>
    <table>
        <tbody>
            <tr>
                <td></td><td colspan="2" class="red"></td>
            </tr>
            <tr>
                <td><label for="role">Role</label></td>
                <td>
                    <%= Html.dropDownList("role_id", Integer.toString(user.role_id), Role.getInstance().getListData()) %>
                </td>
                <td class="red"></td>
            </tr>
            <tr>
                <td></td><td><input type="submit" value="Ubah" /></td>
            </tr>
        </tbody>
    </table>
</form>
<% } %>

<form action="<%=request.getContextPath()%>/user/update?id=<%=user.id%>" method="POST">
    <input type="hidden" name="update_type" value="info"/>
    <table>
        <tbody>
            <tr>
                <td></td><td colspan="2" class="red"><%=user.getErrorGlobal()%></td>
            </tr>
            <tr>
                <td><label for="nama_lengkap">Nama Lengkap</label></td>
                <td><input type="text" name="nama_lengkap" id="nama_lengkap" value="<%=user.nama_lengkap%>"></td>
                <td class="red"><%=user.getError("nama_lengkap")%></td>
            </tr>
            <tr>
                <td><label for="nama_panggilan">Nama Panggilan</label></td>
                <td><input type="text" name="nama_panggilan" id="nama_panggilan" value="<%=user.nama_panggilan%>"></td>
                <td class="red"><%=user.getError("nama_panggilan")%></td>
            </tr>
            <tr>
                <td><label for="email">Email</label></td>
                <td><input type="text" name="email" id="email" value="<%=user.email%>"></td>
                <td class="red"><%=user.getError("email")%></td>
            </tr>
            <tr>
                <td><label for="no_telepon">No Telepon</label></td>
                <td><input type="text" name="no_telepon" id="no_telepon" value="<%=user.no_telepon%>"></td>
                <td class="red"><%=user.getError("no_telepon")%></td>
            </tr>
            <tr>
                <td><label for="alamat">Alamat</label></td>
                <td><input type="text" name="alamat" id="alamat" value="<%=user.alamat%>"></td>
                <td class="red"><%=user.getError("alamat")%></td>
            </tr>
            <tr>
                <td></td><td><input type="submit" value="Simpan" /></td>
            </tr>
        </tbody>
    </table>
</form>

<% if(identity.isAdmin() && identity.getUserId() != user.id) { %>
<a href="<%=Tempe.createUrl("user/resetPassword?id=" + user.id)%>">Reset Password</a>
<% } else { %>
<form action="<%=request.getContextPath()%>/user/update?id=<%=user.id%>" method="POST">
    <input type="hidden" name="update_type" value="password"/>
    <table>
        <tbody>
            <% if (!gantiPassword.message.isEmpty()) { %>
            <tr>
                <td></td><td colspan="2" class="green"><%=gantiPassword.message %></td>
            </tr>
            <% } %>
            <tr>
                <td><label for="password_lama">Password Lama</label></td>
                <td><input type="password" name="password_lama" id="password_lama" value="<%=gantiPassword.password_lama%>"></td>
                <td class="red"><%=gantiPassword.getError("password_lama")%></td>
            </tr>
            <tr>
                <td><label for="password_baru">Password Baru</label></td>
                <td><input type="password" name="password_baru" id="password_baru" value="<%=gantiPassword.password_baru%>"></td>
                <td class="red"><%=gantiPassword.getError("password_baru")%></td>
            </tr>
            <tr>
                <td><label for="re_password_baru">Re Password Baru</label></td>
                <td><input type="password" name="re_password_baru" id="re_password_baru" value="<%=gantiPassword.re_password_baru%>"></td>
                <td class="red"><%=gantiPassword.getError("re_password_baru")%></td>
            </tr>
            <tr>
                <td></td><td><input type="submit" value="Ubah" /></td>
            </tr>
        </tbody>
    </table>
</form>
<% } %>